--
-- Author: shunguo.chen
-- Date: 2019-04-03 15:30:37
--
-- LoginErrDialog 登录错误消息框
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.DialogBase)

local DISABLE_TIME_ARY = {0,2,2,10,2,2,2,Constant.LOGIN_DISABLE_MAX_TIME}

function ClassRef:addClickEvent()
    self._confirmNum = 1

    if not self._okBtn then return end
    self._okBtn:addClickEventListener(function(sender)
        self._confirmNum = self._confirmNum + 1
        executeFunc(self._onConfirm)
    end)

    if not self._cancelBtn then return end
    if self._onCancel then
        self._cancelBtn:addClickEventListener(function(sender)
            executeFunc(self._onCancel)
            if not cc.isDead( self ) then
                self:removeFromParent()
            end
        end)
    else
        self._cancelBtn:setVisible(false)
        self._okBtn:setPositionX(self._ui.bg:getPositionX())
    end
end

function ClassRef:refreshConfirmBtn()
    if self._okBtn then
        display.stopBtnTimeout(self._okBtn)

        local timeout = DISABLE_TIME_ARY[self._confirmNum] or DISABLE_TIME_ARY[#DISABLE_TIME_ARY]
        if self._confirmNum == 1 or self._confirmNum >= #DISABLE_TIME_ARY  then
            local key = Enums.UserDefaultKey.LoginFailTime
            if self._data == 2 then
                key = key .. "_" .. (app.session:getZoneId() or 1)
            end
            local curTime = math.floor(app:getTime())

            if self._confirmNum == 1 then
                local lastTime = app:getValue(key, 0)
                if (curTime - lastTime) < Constant.LOGIN_DISABLE_MAX_TIME then
                    timeout = Constant.LOGIN_DISABLE_MAX_TIME - (curTime - lastTime)
                end
            else
                app:saveValue(key, curTime)
            end
        end

        if timeout > 0 then
            display.setBtnTimeout(self._okBtn, timeout)
        end
        return timeout
    end
    return 0
end

return ClassRef